#!/bin/bash

echo "=== MySQL MCP 服务启动脚本 ==="

# 检查Java是否安装
if ! command -v java &> /dev/null; then
    echo "❌ 错误: 未找到Java，请先安装Java 21或更高版本"
    exit 1
fi

# 检查Maven是否安装
if ! command -v mvn &> /dev/null; then
    echo "❌ 错误: 未找到Maven，请先安装Maven"
    exit 1
fi

echo "✅ Java和Maven已安装"

# 编译项目
echo "📦 编译项目..."
mvn clean compile

if [ $? -ne 0 ]; then
    echo "❌ 编译失败"
    exit 1
fi

echo "✅ 编译成功"

# 启动服务
echo "🚀 启动MySQL MCP服务..."
echo "服务将在 http://localhost:8099 启动"
echo "按 Ctrl+C 停止服务"
echo ""

# 在后台启动服务
mvn spring-boot:run &
SERVICE_PID=$!

# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 10

# 检查服务是否启动成功
if curl -s http://localhost:8099/health > /dev/null; then
    echo "✅ 服务启动成功！"
    echo ""
    echo "=== 可用端点 ==="
    echo "健康检查: http://localhost:8099/health"
    echo "数据库状态: http://localhost:8099/health/database"
    echo "MCP接口: http://localhost:8099/mcp/call"
    echo ""
    echo "=== 测试客户端 ==="
    echo "Node.js测试: npm install && npm test"
    echo "Python测试: pip install -r requirements.txt && python test_client.py"
    echo ""
    
    # 等待用户中断
    wait $SERVICE_PID
else
    echo "❌ 服务启动失败"
    kill $SERVICE_PID 2>/dev/null
    exit 1
fi 